//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BSMOperator.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (Cephei::QL::Math::IArray^ grid, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double residualTime) : CTridiagonalOperator(CBSMOperator::typeid)
{
    CArray^ _Cgrid;
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phBSMOperator = NULL;
#endif
        _Cgrid = safe_cast<CArray^> (grid);
        _Cgrid->Lock();
        QuantLib::Array& _grid = static_cast<QuantLib::Array&> (_Cgrid->GetReference ()); 
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Time _residualTime = (QuantLib::Time)ValueHelper::Convert (residualTime); //d
        _ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (new QuantLib::BSMOperator ( _grid,  _process,  _residualTime ));
        SetTridiagonalOperator (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (UInt64 size, Double dx, Double r, Double q, Double sigma) : CTridiagonalOperator(CBSMOperator::typeid)
{
    try
    {
#ifdef HANDLE
        _phBSMOperator = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //d
        QuantLib::Real _dx = (QuantLib::Real)ValueHelper::Convert (dx); //d
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //d
        QuantLib::Rate _q = (QuantLib::Rate)ValueHelper::Convert (q); //d
        QuantLib::Volatility _sigma = (QuantLib::Volatility)ValueHelper::Convert (sigma); //d
        _ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (new QuantLib::BSMOperator ( _size,  _dx,  _r,  _q,  _sigma ));
        SetTridiagonalOperator (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator () : CTridiagonalOperator(CBSMOperator::typeid)
{
    try
    {
#ifdef HANDLE
        _phBSMOperator = NULL;
#endif
        _ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (new QuantLib::BSMOperator ( ));
        SetTridiagonalOperator (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (boost::shared_ptr<QuantLib::BSMOperator>& childNative, Object^ owner) : CTridiagonalOperator(CBSMOperator::typeid)
{
#ifdef HANDLE
	_phBSMOperator = NULL;
#endif
	_ppBSMOperator = &childNative;
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (QuantLib::BSMOperator& childNative, Object^ owner) : CTridiagonalOperator(CBSMOperator::typeid)
{
#ifdef HANDLE
	_phBSMOperator = NULL;
#endif
	_ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (&childNative);
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
    _BSMOperatorOwner = owner;
    _TridiagonalOperatorOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (CBSMOperator^ copy) : CTridiagonalOperator(CBSMOperator::typeid)
{
#ifdef HANDLE
	_phBSMOperator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (copy->GetShared());
        _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
    }
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (PLATFORM::Type^ t) : CTridiagonalOperator(CBSMOperator::typeid)
{
#ifdef HANDLE
	_phBSMOperator = NULL;
#endif
	if (!t->IsSubclassOf(CBSMOperator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (QuantLib::Handle<QuantLib::BSMOperator>& childNative, Object^ owner)  : CTridiagonalOperator(CBSMOperator::typeid)
{
	_phBSMOperator = &childNative;
	_ppBSMOperator = &static_cast<boost::shared_ptr<QuantLib::BSMOperator>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
    _BSMOperatorOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (QuantLib::Handle<QuantLib::BSMOperator> childNative)  : CTridiagonalOperator(CBSMOperator::typeid)
{
	_phBSMOperator = &childNative;
	_ppBSMOperator = &static_cast<boost::shared_ptr<QuantLib::BSMOperator>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CBSMOperator::CBSMOperator (QuantLib::BSMOperator childNative)  : CTridiagonalOperator(CBSMOperator::typeid)
{
#ifdef HANDLE
	_phBSMOperator = NULL;
#endif
	_ppBSMOperator = new boost::shared_ptr<QuantLib::BSMOperator> (new QuantLib::BSMOperator (childNative));
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppBSMOperator));
}
#endif

Cephei::QL::Methods::Finitedifferences::CBSMOperator::~CBSMOperator ()
{
    if (_ppBSMOperator != NULL)
    {
	    delete _ppBSMOperator;
        _ppBSMOperator = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CBSMOperator::!CBSMOperator ()
{
    if (_ppBSMOperator != NULL)
    {
	    delete _ppBSMOperator;
    }
}
QuantLib::BSMOperator& Cephei::QL::Methods::Finitedifferences::CBSMOperator::GetReference ()
{
    if (_ppBSMOperator == NULL) throw REFNEW NativeNullException ();
	return **_ppBSMOperator;
}
boost::shared_ptr<QuantLib::BSMOperator>& Cephei::QL::Methods::Finitedifferences::CBSMOperator::GetShared ()
{
    if (_ppBSMOperator == NULL) throw REFNEW NativeNullException ();
	return *_ppBSMOperator;
}
QuantLib::BSMOperator* Cephei::QL::Methods::Finitedifferences::CBSMOperator::GetPointer ()
{
    if (_ppBSMOperator == NULL) throw REFNEW NativeNullException ();
	return &**_ppBSMOperator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BSMOperator>& Cephei::QL::Methods::Finitedifferences::CBSMOperator::GetHandle ()
{
	if (_phBSMOperator == NULL)
	{
		_phBSMOperator = new Handle<QuantLib::BSMOperator> (*_ppBSMOperator);
	}
	return *_phBSMOperator;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CBSMOperator::HasNative () 
{
	return (_ppBSMOperator != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IBSMOperator^ Cephei::QL::Methods::Finitedifferences::CBSMOperator_Factory::Create (Cephei::QL::Math::IArray^ grid, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double residualTime)
{
    return REFNEW CBSMOperator ( grid,  process,  residualTime);
}
Cephei::QL::Methods::Finitedifferences::IBSMOperator^ Cephei::QL::Methods::Finitedifferences::CBSMOperator_Factory::Create (UInt64 size, Double dx, Double r, Double q, Double sigma)
{
    return REFNEW CBSMOperator ( size,  dx,  r,  q,  sigma);
}
Cephei::QL::Methods::Finitedifferences::IBSMOperator^ Cephei::QL::Methods::Finitedifferences::CBSMOperator_Factory::Create ()
{
    return REFNEW CBSMOperator ();
}
